#!/usr/bin/env python3
#
# 50-maas-01-commissioning - Gather all commissioning data
#
# Copyright (C) 2012-2020 Canonical
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# --- Start MAAS 1.0 script metadata ---
# name: 50-maas-01-commissioning
# title: Gather all commissioning data
# description: Gather all commissioning data
# script_type: commissioning
# timeout: 60
# --- End MAAS 1.0 script metadata ---

import json
import os
import sys


def read_json_file(path):
    try:
        with open(path) as fd:
            return json.load(fd)
    except OSError as e:
        sys.exit(f"Failed to read {path}: {e}")
    except json.JSONDecodeError as e:
        sys.exit(f"Failed to parse {path}: {e}")


data = read_json_file(os.environ["MAAS_RESOURCES_FILE"])

# add custom storage configuration if present
storage_path = os.environ.get("MAAS_STORAGE_CONFIG_FILE")
if storage_path and os.path.exists(storage_path):
    storage_config = read_json_file(storage_path)
    data["storage-extra"] = storage_config

print(json.dumps(data, indent=2))
